<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/21 0021
 * Time: 20:17
 */

namespace app\admin\controller;
use think\Controller;
use think\Db;

class Main extends Bases{

    public function _initialize(){
        if(!session('id')){
            $this->redirect('/admin/index/index');
        }
    }
     //首页
    public function index(){
        //咨询
        $count= Db::name('consult')->where('status=0 and is_del=0')->count();
        $zx_time =  Db::name('consult')->where('status=0 and is_del=0 and sign="pc"')->max('create_time');
        if($zx_time){
            $fen_=intval((time()-$zx_time)/60).'分钟';
            if($fen_ > 60 && $fen_ <=60*24){
                $fen_ =intval((time()-$zx_time)/(60*60)).'小时';
            }else if($fen_ >60*24){
                $fen_ =intval((time()-$zx_time)/(60*60*24)).'天';
            }
        }
        //当前账号
        $data = Db::name('admin')->where('id',session('id'))->field('username')->find();
        $this->assign('data',$data);
        //前台链接
        $this->assign('url','/index.php');
        $this->assign('url_m','/wap.php');


        //账号身份
        $is_vip = Db::name('admin')->where('id',session('id'))->value('is_vip');
        $this->assign('is_vip',$is_vip);
        $this->assign('fen_',isset($fen_)?$fen_:'');
        $this->assign('count',$count);


        return $this->fetch();
    }


    //主页
    public function index_v1(){
        $data =Db::name('admin')->where('id',session('id'))->find();
        //本月咨询
        $BeginDate=date('Y-m-01');
        $firstDay=strtotime($BeginDate);
        $lastDay=strtotime(date('Y-m-d', strtotime("$BeginDate +1 month -1 day")));
        $where1['create_time']=['between',[$firstDay,$lastDay]];
        $where1['is_del']=0;

        //本月咨询
        $count_zx = Db::name('consult')->where($where1)->count();
        $this->assign('count_zx',$count_zx);
        //待处理咨询
        $where2['status']=0;
        $where2['is_del']=0;
        $count_dzx = Db::name('consult')->where($where2)->count();
        $this->assign('count_dzx',$count_dzx);
        //客照总数
        $where3['is_del']=0;
        $where3['sign']="pc";
        $count_works=Db::name('works')->where($where3)->count();
        $this->assign('count_works',$count_works);
        //作品总数
        $count_zp=Db::name('zp')->where($where3)->count();
        $this->assign('count_zp',$count_zp);
        //基地场馆
        $count_psjd=Db::name('psjd')->where($where3)->count();
        $this->assign('count_psjd',$count_psjd);
        //全球旅拍
        $count_qqlp=Db::name('qqlp')->where($where3)->count();
        $this->assign('count_qqlp',$count_qqlp);

        //艺术会所
        $count_yshs=Db::name('yshs')->where($where3)->count();
        $this->assign('count_yshs',$count_yshs);
        //活动/新品
        $count_activity=Db::name('activity')->where($where3)->count();
        $this->assign('count_activity',$count_activity);
        //新闻资讯
        $count_news=Db::name('news')->where($where3)->count();
        $this->assign('count_news',$count_news);
        //婚嫁知识

        $count_article=Db::name('article')->where($where3)->count();
        $this->assign('count_article',$count_article);
        //广告总数
        $count_banner=Db::name('banner')->where($where3)->count();
        $this->assign('count_banner',$count_banner);
        //回收总数据
        $where4['is_del']=1;
        $where4['sign']="pc";
        $count1= Db::name('consult')->where($where4)->count();
        $count2= Db::name('works')->where($where4)->count();
        $count3= Db::name('zp')->where($where4)->count();
        $count4= Db::name('psjd')->where($where4)->count();
        $count5= Db::name('qqlp')->where($where4)->count();
        $count6= Db::name('yshs')->where($where4)->count();
        $count7= Db::name('activity')->where($where4)->count();
        $count8= Db::name('news')->where($where4)->count();
        $count9= Db::name('article')->where($where4)->count();
        $count = $count1+$count2+$count3+$count4+$count5+$count6+$count7+$count8+$count9;
        $this->assign('count',$count);
       /* $this->assign('count1',$count1);
        $this->assign('count2',$count2);
        $this->assign('count3',$count3);
        $this->assign('count4',$count4);
        $this->assign('count5',$count5);
        $this->assign('count6',$count6);
        $this->assign('count7',$count7);
        $this->assign('count8',$count8);
        $this->assign('count9',$count9);*/

        $this->assign('data',$data);
        return $this->fetch();
    }
    //404
    public function error_404(){

        return $this->fetch();
    }
    //修改密码
    public function editCode(){
        $data = session('admin');
        $this->assign('username',$data['username']);
        return $this->fetch('editcode');
    }
    //确认修改
    public function subEditcode(){
        $data = session('admin');
        $password = input('post.password');
        $new_password = input('post.new_password');
        if(md5($password) != $data['password']){
            return  json(['code'=>0,'msg'=>'旧密码验证错误']);
        }
        $map['password'] = md5($new_password);
        $id = session('id');
        $result = Db::name('admin')->where('id',$id)->update($map);
        if($result !==false){
            return json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

}